﻿/**
 * XML Loader
 * This class load an xml file
 *
 * @author: Veronica Liesaputra
 */
class RealisticBooks.XMLLoader {
/**
 * The xml node
 * @type XML
 */
private var xmlNode:XML;
/**
 * Initialize the XML loader
 * @constructor
 * Parameters:
 * @param {Function} 	parse_xml   the function for parsing the xml
 * @param {Function} 	error_xml   the function when xml is not loaded
 * @param {Object}		obj			the instance with both parse_xml and error_xml
 */
public function XMLLoader (parse_xml:Function, error_xml:Function, obj:Object) {
	var xmlNode:XML = new XML();
	xmlNode.ignoreWhite = false;//ignore white space
	xmlNode.onLoad = function(success:Boolean) {	
		if (success == true) {
			var xmlStatus:Number = xmlNode.status;
			if (xmlStatus != 0) {
				error_xml(xmlStatus, obj);
			} else {
				parse_xml(xmlNode, obj);//parse XML tree
			}
		}
		else {
			error_xml(-1, obj);
		}
	};
	this.xmlNode = xmlNode;
}
/**
 * Load the XML file
 * Parameters:
 * @param {String} 	xml_url 	the url of the xml file
 * @param {Boolean} on_server	whether the program is run on a webserver
 */
public function loadXML (xml_url:String,on_server:Boolean):Void {
	//Append the url with a timestamp and cachebuster so that
 	//it will always reload the file not just the cached version
	if (on_server == true) {
 		
  		var sTimestamp:String = "&timestamp=" + new Date().getTime();
  		xml_url += "?cachebuster=" + Math.random() + sTimestamp;
	}
	this.xmlNode.load(xml_url);
}
/**
 * Clear the XML node
 */
public function clearXML ():Void {
	this.xmlNode.removeNode();
}
/**
 * Get the error message when loading the XML file based on the
 * error number
 * Parameters:
 * @param {Number} xmlStatus the error number
 * Returns:
 * @return {String} the error message
 */
public static function errorMessageXML (xmlStatus:Number):String {
	var errorMessage:String;
    switch (xmlStatus) {
		case -1 :
			errorMessage = "Cannot find the XML document!";
			break;
    	case -2 :
        	errorMessage = "A CDATA section was not properly terminated.";
        	break;
    	case -3 :
        	errorMessage = "The XML declaration was not properly terminated.";
        	break;
    	case -4 :
        	errorMessage = "The DOCTYPE declaration was not properly terminated.";
        	break;
    	case -5 :
        	errorMessage = "A comment was not properly terminated.";
        	break;
    	case -6 :
        	errorMessage = "An XML element was malformed.";
        	break;
    	case -7 :
        	errorMessage = "Out of memory.";
        	break;
    	case -8 :
        	errorMessage = "An attribute value was not properly terminated.";
        	break;
    	case -9 :
        	errorMessage = "A start-tag was not matched with an end-tag.";
        	break;
    	case -10 :
        	errorMessage = "An end-tag was encountered without a matching start-tag.";
        	break;
    	case -11:
			errorMessage = "The total page number has to be an even number.";
			break;
		default :
        	errorMessage = "An unknown error has occurred.";
        	break;
	}
	
	return "Error: " + errorMessage;
}
}